﻿Imports ACompany.OrderProcessing.Presentation
Imports ACompany.OrderProcessing.Model
Imports System.Collections.ObjectModel

Partial Class CreateOrder
    Inherits System.Web.UI.Page
    Implements ICreateOrderView

    Private _Presenter As CreateOrderPresenter

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        _Presenter = New CreateOrderPresenter(Me)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            _Presenter.Display()
        End If
    End Sub

    Protected Sub btnAddToOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddToOrder.Click

        _Presenter.AddProductBy(New Guid(Me.ddlProducts.SelectedValue))
    End Sub

    Protected Sub btnPlaceOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlaceOrder.Click

        _Presenter.PlaceOrder()
    End Sub

    Public Sub ErrorMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ErrorMessage
        If String.IsNullOrEmpty(Message) Then
            pnlError.Visible = False
        Else
            pnlError.Visible = True
        End If
        Me.litErrorMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub InfoMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.InfoMessage
        If String.IsNullOrEmpty(Message) Then
            pnlInfo.Visible = False
        Else
            pnlInfo.Visible = True
        End If
        Me.litInfoMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub ViewTitle(ByVal title As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ViewTitle
        Me.litTitle.Text = title
    End Sub

    Public WriteOnly Property Customers() As System.Collections.Generic.List(Of ACompany.OrderProcessing.Model.Customer) Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.Customers
        Set(ByVal value As System.Collections.Generic.List(Of ACompany.OrderProcessing.Model.Customer))
            Me.ddlCustomers.DataSource = value
            ddlCustomers.DataTextField = "Name"
            ddlCustomers.DataValueField = "Id"
            Me.ddlCustomers.DataBind()
        End Set
    End Property

    Public ReadOnly Property Order() As ACompany.OrderProcessing.Model.Order Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.Order
        Get
            Dim CurrentOrder As Object = Session("CurrentOrder")

            If CurrentOrder Is Nothing Then
                CurrentOrder = New Order

                Session("CurrentOrder") = CurrentOrder
            End If

            Return CType(Session("CurrentOrder"), Order)
        End Get
    End Property

    Public WriteOnly Property Products() As System.Collections.Generic.List(Of ACompany.OrderProcessing.Model.Product) Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.Products
        Set(ByVal value As System.Collections.Generic.List(Of ACompany.OrderProcessing.Model.Product))
            Me.ddlProducts.DataSource = value
            Me.ddlProducts.DataTextField = "Name"
            Me.ddlProducts.DataValueField = "Id"
            Me.ddlProducts.DataBind()
        End Set
    End Property


    Public WriteOnly Property City() As String Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.City
        Set(ByVal value As String)
            Me.litCity.Text = value
        End Set
    End Property

    Public WriteOnly Property County() As String Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.County
        Set(ByVal value As String)
            Me.litCounty.Text = value
        End Set
    End Property

    Public WriteOnly Property CustomerName() As String Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.CustomerName
        Set(ByVal value As String)
            litCustomerName.Text = value
        End Set
    End Property

    Public WriteOnly Property PostCode() As String Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.PostCode
        Set(ByVal value As String)
            Me.litPostCode.Text = value
        End Set
    End Property

    Public WriteOnly Property Street() As String Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.Street
        Set(ByVal value As String)
            Me.litStreet.Text = value
        End Set
    End Property

    Public WriteOnly Property OrderItems() As ReadOnlyCollection(Of ACompany.OrderProcessing.Model.Item) Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.OrderItems
        Set(ByVal value As ReadOnlyCollection(Of ACompany.OrderProcessing.Model.Item))
            Me.grdOrderItems.DataSource = value
            Me.grdOrderItems.DataBind()
        End Set
    End Property

    Public Sub clearOrder() Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.clearOrder
        Session("CurrentOrder") = Nothing
    End Sub

    Protected Sub btnSelectCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectCustomer.Click
        _Presenter.SetCustomerBy(New Guid(Me.ddlCustomers.SelectedValue))
    End Sub

    Public WriteOnly Property SelectCustomerButtonEnabled() As Boolean Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.SelectCustomerButtonEnabled
        Set(ByVal value As Boolean)
            Me.btnSelectCustomer.Enabled = value
        End Set
    End Property

    Public WriteOnly Property SelectProductButtonEnabled() As Boolean Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.SelectProductButtonEnabled
        Set(ByVal value As Boolean)
            Me.btnAddToOrder.Enabled = value
        End Set
    End Property

    Public WriteOnly Property CreateOrderButtonEnabled() As Boolean Implements ACompany.OrderProcessing.Presentation.ICreateOrderView.CreateOrderButtonEnabled
        Set(ByVal value As Boolean)
            Me.btnPlaceOrder.Enabled = value
        End Set
    End Property
End Class
